package com.addplus.townmall.server.queue_consumer.consumer;

import com.addplus.townmall.server.api.constant.StringConstant;
import com.addplus.townmall.server.api.mapper.town.AppMessageRecordMapper;
import com.addplus.townmall.server.api.model.town.AppMessageRecord;
import com.addplus.townmall.server.api.model.town.param.AliyunMsmSingleParam;
import com.addplus.townmall.server.api.utils.DataUtils;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 类名: AliyunMsmConsumer
 *
 * @author zhangjiehang
 * @version V1.0
 * @date 2018/9/27 下午5:52
 * @description 类描述: 阿里云发送短信消费者
 */
@Component
@RabbitListener(queues = "msm_queue")
public class AliyunMsmConsumer {

    @Value("${sms.accessKeyId}")
    private String accessKeyId;

    @Value("${sms.accessKeySecret}")
    private String accessKeySecret;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AppMessageRecordMapper appMessageRecordMapper;

    @RabbitHandler
    public void process(AliyunMsmSingleParam aliyunMsmSingleParam) throws Exception {
        if (aliyunMsmSingleParam == null || aliyunMsmSingleParam.getPlatformEnum() == null ||
                DataUtils.isEmpty(aliyunMsmSingleParam.getPhone(), aliyunMsmSingleParam.getCode()) ||
                DataUtils.EmptyOrNegative(aliyunMsmSingleParam.getType())) {
            return;
        }
        IClientProfile profile = DefaultProfile.getProfile(StringConstant.ALIYUN_SMS_REGIONID, accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint(StringConstant.ALIYUN_SMS_REGIONID, StringConstant.ALIYUN_SMS_REGIONID, StringConstant.ALIYUN_SMS_PRODUCT, StringConstant.ALIYUN_SMS_DOMAIN);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(aliyunMsmSingleParam.getPhone());
        //必填:短信签名-可在短信控制台中找到
        request.setSignName(StringConstant.ALIYUN_SMS_SIGNNAME);
        //必填:短信模板-可在短信控制台中找到
        // TODO: 2018/9/20 修改成对应阿里云的短信模板
        switch (aliyunMsmSingleParam.getType()) {
            case 0:
                request.setTemplateCode("SMS_100170102");
                break;
            case 1:
                request.setTemplateCode("SMS_100170104");
                break;
            case 2:
                request.setTemplateCode("SMS_100170101");
                break;
            case 3:
                request.setTemplateCode("SMS_100170104");
                break;
            case 4:
                request.setTemplateCode("SMS_100170100");
                break;
            case 5:
                request.setTemplateCode("SMS_117525560");
                break;
            case 6:
                request.setTemplateCode("SMS_100170101");
                break;
            default:
                request.setTemplateCode("SMS_100170102");
                break;
        }
        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", aliyunMsmSingleParam.getCode());
        request.setTemplateParam(jsonObject.toJSONString());
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        //存入redis
        String context = MessageFormat.format(StringConstant.MESSAGE_VERIFICATION_CODE, aliyunMsmSingleParam.getType(),
                aliyunMsmSingleParam.getPlatformEnum().getType(), aliyunMsmSingleParam.getPhone());
        redisTemplate.opsForValue().set(context, aliyunMsmSingleParam.getCode(), 10, TimeUnit.MINUTES);
        //记录发送状态
        AppMessageRecord appMessageRecord = new AppMessageRecord();
        appMessageRecord.setIsDeleted(0);
        appMessageRecord.setPlatform(aliyunMsmSingleParam.getPlatformEnum().getType());
        appMessageRecord.setType(aliyunMsmSingleParam.getType());
        appMessageRecord.setPhone(aliyunMsmSingleParam.getPhone());
        appMessageRecord.setVerificationCode(aliyunMsmSingleParam.getCode());
        Date date = new Date();
        appMessageRecord.setGmtCreate(date);
        appMessageRecord.setGmtModified(date);
        BeanUtils.copyProperties(sendSmsResponse, appMessageRecord);
        appMessageRecordMapper.insert(appMessageRecord);
    }
}
